﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CashFlow.Common;
using Should.Fluent;

namespace CashFlow.Common.Tests
{
	[TestClass]
	public class DateTimeUtilTest
	{
		[TestMethod]
		public void ParseYYMM_Returns010110_From1001()
		{
			var util = new DateTimeUtil();

			var actual = util.ParseYYMM("1001");

			Assert.AreEqual(new DateTime(2010, 1, 1), actual);
		}

		[TestMethod]
		public void ToYYMM_From10202011_Returns1110()
		{
			var util = new DateTimeUtil();
			var date = new DateTime(2011, 10, 20);

			var actual = util.ToYYMM(date);

			Assert.AreEqual("1110", actual);
		}



		/// <summary>
		///A test for GetMonths
		///</summary>
		[TestMethod()]
		public void GetMonths_Same_Months_One_Date_Returned()
		{
			var target = new DateTimeUtil();
			var from = new DateTime(2010, 10, 5);
			var to = new DateTime(2010, 10, 20);
			var expected = new List<DateTime> { new DateTime(2010, 10, 1) };
			var actual = target.GetMonths(from, to);
			CheckMonths(expected, actual);
		}

		[TestMethod()]
		public void GetMonths_3_Months_3_Dates_Returned()
		{
			var target = new DateTimeUtil();
			var from = new DateTime(2009, 11, 5);
			var to = new DateTime(2010, 1, 20);
			var expected = new List<DateTime> { 
				new DateTime(2009, 11, 1 ),
				new DateTime(2009, 12, 1 ),
				new DateTime(2010, 1, 1 ) 
			};
			var actual = target.GetMonths(from, to);
			CheckMonths(expected, actual);
		}

		[TestMethod()]
		public void GetMonths_From_GreateThanTo_Returs_Empty_List()
		{
			var target = new DateTimeUtil();
			var from = new DateTime(2010, 10, 5);
			var to = new DateTime(2010, 1, 2);
			var expected = new List<DateTime>();
			var actual = target.GetMonths(from, to);
			actual.Count().Should().Equal(actual.Count());
			CheckMonths(expected, actual);
		}

		private static void CheckMonths(List<DateTime> expected, IEnumerable<DateTime> actual)
		{
			actual.Count().Should().Equal(actual.Count());
			for (int i = 0; i < actual.Count(); i++)
			{
				actual.ElementAt(i).Date.Should().Equal(expected.ElementAt(i).Date);
			}
		}


		/// <summary>
		///A test for MonthLastDate
		///</summary>
		[TestMethod()]
		public void MonthLastDate_For_20101007_Returns_20101031()
		{
			var target = new DateTimeUtil();
			var value = new DateTime(2010, 10, 7);
			var expected = new DateTime(2010, 10, 31);
			var actual = target.MonthLastDate(value);
			actual.Date.Should().Equal(expected.Date);
		}

		[TestMethod()]
		public void MonthLastDate_For_20120207_Returns_20120229()
		{
			var target = new DateTimeUtil();
			var value = new DateTime(2012, 2, 7);
			var expected = new DateTime(2012, 2, 29);
			var actual = target.MonthLastDate(value);
			actual.Date.Should().Equal(expected.Date);
		}
	}
}
